{% extends "base.html" %}
{% block head %}
    <meta charset="UTF-8">
    <title>测试管理系统</title>
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <script src="../static/layui/layui.js"></script>
    <!--     <script type="text/javascript">try { Typekit.load(); } catch (e) { }</script> -->
    <script type="text/javascript" src="../static/js/tool_p.js"></script>
    <script src="../static/js/jquery-1.11.3.min.js"></script>
    <!-- <script type="text/javascript" src="/cont-inspec/scripts/jquery.form.js"></script> -->
    <link href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" rel='stylesheet'/>
    <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
{% endblock head %}
{% block left %}
    {{ super() }}
{% endblock left %}
{% block right %}
    {{ super() }}
    <!-- 修改name（页面名称） -->
    {% set name = '计划任务' %}
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>{{ name }}</legend>
    </fieldset>
    <!-- 页面私有部分 -->
    <style>
        #table2 {
            border: hidden
        }

        #table2 tr {
            height: 60px
        }

        #sure {
            font-size: 18px;
            color: #006600;
            width: 70px
        }

    </style>
    <script type="text/javascript">

        $(function (){
            file = function(){
                var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
                if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
                        alert("请选择文件");                  
                        return;
               }
                var name = document.getElementById("FileUpload");
               if(typeof(fileObj) =="undefined" || fileObj.size <= 0){
                    alert("请选择文件");
                    return;
               } 
            var formFile = new FormData();
            formFile.append("action","/resources/taskupload");
            formFile.append("file",fileObj);
            var data = formFile;
            $.ajax({
                url:"/resources/taskupload",
                data:data,
                type:"post",
                dataType:"text",
                cache:false,
                processData: false,   // jQuery不要去处理发送的数据
                contentType: false,
                success:function(res){
                    if(res == "1"){
                        alert("上传成功！");
                        name.value = '';
                    }
                },
                error:function (jqXHR, textStatus, errorThrown) {
                      /*弹出jqXHR对象的信息*/
                      alert("上传失败！");
                }
            })



            }
        })
        // 添加任务
        $(function(){
            add = function(){
            var name = document.getElementById("name").value;
            var cron = document.getElementById("cron").value;
            if (name == ""){
                alert("请输入任务名称")
                return;
            }
            if(cron == ""){
                alert("请输入cron内容")
                return;
            }
            $.ajax({
                url:'inserttask',
                data:{
                    'name':name,
                    'cron':cron
                },
                type:'POST',
                dataType:'json',
                success:function(res){
                    if(res==1){
                        alert("添加成功！");
                        location.reload();

                    }
                    },
                    error:function(jqXHR, textStatus, errorThrown){

                        alert("添加失败！");
                    }
              })
            }

        })

        //删除数据
        $(function(){
            dele = function(id,status){
            if(status == '停止'){
                alert("请停止任务后删除！")
                return;
            }
            $.ajax({
                url:'deltask',
                data:{
                    'id':id,
                    'begin':status
                },
                type:'POST',
                dataType:'json',
                success:function(res){
                    if(res == 1){
                        alert("删除成功！")
                        location.reload();
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    alert("删除失败")
                }
             })
            }

        })

        // 编辑
        function selectRole(id,name,cron,status){
            if(status == '停止'){
                alert("请停止任务后编辑！")
                return;
            }
            layui.use(['layer','form'],function(){
            var form =layui.form;
            var layer = layui.layer;
            layer.open({
            //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
            type:1,
            title:"修改",
            area: ['35%','40%'],
            btn: ['确定', '取消'],
            btnAlign:'c',
            skin: 'layui-layer-molv',
            content:$("#popSearchRoleTest").html(),
            yes:function(index,layero){
                a = layero.find('#ename');
                a1 = $(a).val();
                b = layero.find("#ecron");
                b1 =$(b).val()
                if(a1==""){
                    alert("请输入任务名称！")
                    return false;
                }
                if(b1==""){
                    alert("请输入cron内容！")
                    return false;
                }

                $.ajax({
                url:"edittask",
                data:{
                    'id1':id,
                    'name1':a1,
                    'cron1':b1
                },
                type:'POST',
                dataType:'json',
                success:function(res){
                    if(res == 1){
                        alert("修改成功！")
                        location.reload();
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    alert("修改失败！")
                }

            })
            }
            });
            form.val("formTestFilter2121",{"ename":name,"ecron":cron})
            })
        }

        //启动/停止操作
        $(function(){
            clibegin = function (id,task_cron,status) {

            $.ajax({
                url:'runstop',
                data:{
                    'status':status,
                    'id':id,
                    'task_cron':task_cron
                },
                type:'POST',
                dataType:'json',
                success:function(res){
                    if(res == 1){
                        alert("成功！")                        
                        location.reload();
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    alert("失败！")
                }
             })
            }
        })
    </script>
<body>
    <form action="" method="POST">
        <table border="0px" id="table2" cellspacing="10">
            <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;上传脚本</td>
                <td ><input type="file" name="FileUpload" id="FileUpload">
                <a class="layui-btn layui-btn-mini" id="btn_uploadimg" onclick="file()">上传文件</a> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF0000;font-size:15px;" ></td>
            </tr>
            <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;任务名称&nbsp;&nbsp;</td>
                <td style="width:400px"><input class="layui-input" type="text" id='name' name="name" placeholder="请输入任务名称"></td>
            </tr>
            <br/>
            <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron</td>
                <td><textarea type="text" class="layui-input" name="cron" id="cron" placeholder="请输入cron内容" style="height:120px"></textarea></td>
                <td><font color="green" size="2px">&nbsp;&nbsp;&nbsp;&nbsp;cron说明：<br/>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格式为：<span style="color:red">* * * * *</span> cd /data/logs/cron/ && /root/.pyenv/shims/python <span style="color:red">xxx.py</span> >> /data/logs/cron/<span style="color:red">xxx.log</span> 2>&1；<br/>
                    &nbsp;&nbsp;&nbsp;&nbsp;*****代表：分、小时、日、月、星期；<br/>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xxx.py：为脚本名称；</font></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="button" id="sure" value="添加任务" onclick="return add();" class="layui-btn layui-btn-xs" style="color: #FFFFFF;width:80px;font-size:15px; height:30px">
                </td>
                <td></td>
            </tr>  
        </table>
<hr class="layui-bg-green">
</br>
        <table cellspacing="0" cellpadding="0" border="0" class="layui-table">
        <tbody>
            <tr data-index="0"  class="layui-table-click">
                <td>ID</td>
                <td>任务名称</td>
                <td style="width:400px;">执行时机</td>
                <td>添加时间</td>
                <td>操作</td>
            </tr>
            {% for i in data %}
            <tr>
                <td>{{ i[0] }}</td>
                <td>{{ i[1] }}</td>
                <td>{{ i[2] }}</td>
                <td>{{ i[3] }}</td>
                <td><input type="button" name="begin" value="{{ i[4] }}" id="begin" onclick="clibegin('{{ i[0] }}','{{i[2]}}','{{ i[4] }}')" class="layui-btn layui-btn-primary layui-btn-xs">
                    <input type="button" name="edit" value="编辑" id="edit" onclick="selectRole('{{i[0]}}','{{ i[1] }}','{{ i[2] }}','{{ i[4] }}')" class="layui-btn layui-btn-xs">
                    <input type="button" name="del" value="删除" id="del" onclick="return dele('{{ i[0] }}','{{ i[4] }}')" class="layui-btn layui-btn-danger layui-btn-xs">
                </td>
            </tr>
            {% endfor %} 
        </tbody>
        </table>
    </form>

<!-- 编辑框 -->
<div class="layui-row" id="popSearchRoleTest" style="display:none;">
        <div class="layui-col-md11">
            <form class="layui-form" lay-filter="formTestFilter2121" >
            <table border="0px" id="table2" cellspacing="10">
            <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;任务名称&nbsp;&nbsp;</td>
                <td style="width:400px"><input class="layui-input" type="text" id='ename' name="ename" value="haha"></td>
            </tr>
            <br/>
            <br/>
            <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron</td>
                <td><textarea type="text" class="layui-input" name="ecron" id="ecron" style="height:100px"></textarea></td>
            </tr>
<!--             <tr>
                <td colspan="8" align="center"><input type="button" id="esure" value="修改" onclick="" class="layui-btn layui-btn-xs" style="color: #FFFFFF;width:90px;font-size:15px; height:38px">
                </td>
            </tr>  --> 
           </table>
           </form>
        </div>
    </div>
</body>
{% endblock right %}
<!-- 此模板现有部分，除页面名称外，其他均不可编辑 -->

